package com.efast.dmom.equipment;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.session.web.http.HttpSessionStrategy;

import com.efast.cafe.commponent.log.ext.CafeLogger;
import com.efast.cafe.commponent.log.ext.CafeLoggerFactory;
import com.efast.cafe.framework.constant.SecurityConstant;
import com.efast.cafe.framework.filter.CsrfFilter;
import com.efast.cafe.framework.filter.HttpLoggingFilter;
import com.efast.cafe.framework.filter.MyAuthenticationFilter;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.framework.session.MyCookieHttpSessionStrategy;
import com.efast.cafe.framework.spring.SpringContextUtils;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableDiscoveryClient //启用服务注册与发现
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@ComponentScan(basePackages = {"com.efast.cafe.framework","com.efast.cafe.commponent","com.efast.dmom.equipment"})
@EnableAsync
@EnableConfigurationProperties
@EnableSwagger2
@EnableFeignClients(basePackages={"com.efast.cafe.portal.remote","com.efast.cafe.msg.remote"})
public class EquipmentApplication extends SpringBootServletInitializer{

//	CafeLogger logger = CafeLoggerFactory.getLogger(getClass());
	
	
	public static void main(String[] args) {
		SpringApplication.run(EquipmentApplication.class, args);
	}
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return super.configure(builder);
	}
	
	
//	@ConditionalOnProperty(name = "cafe.enable.filterLog",havingValue = "true")
//	@Bean
//	public FilterRegistrationBean httpLoggingFilter() {
//		FilterRegistrationBean registration = new FilterRegistrationBean();
//		// 添加过滤器
//		registration.setFilter(new HttpLoggingFilter());
//		// 设置过滤路径，/*所有路径
//		registration.addUrlPatterns("/*");
//		// 设置优先级
//		registration.setName("httpLoggingFilter");
//		// 设置优先级
//		registration.setOrder(3);
//		return registration;
//	}
//	
//	@Bean
//	public FilterRegistrationBean testFilterRegistration() {
//		FilterRegistrationBean registration = new FilterRegistrationBean();
//		List<Pattern> patterns = new ArrayList<Pattern>();
//		patterns.add(Pattern.compile(SecurityConstant.LOGIN_PATH));
//		patterns.add(Pattern.compile(SecurityConstant.LOGOUT_PATH));
//		patterns.add(Pattern.compile("/openHTTP/*"));
//		patterns.add(Pattern.compile("/favicon.ico"));
//		patterns.add(Pattern.compile("/openWS/*"));
//		patterns.add(Pattern.compile("/openSocket/*"));
//		registration.setFilter(myAuthenticationFilter(patterns));//添加过滤器
//		registration.addUrlPatterns("/*");//设置过滤路径，/*所有路径
//		registration.setName("myAuthenticationFilter");//设置优先级
//		registration.setOrder(1);//设置优先级
//		return registration;
//	}
//	
//	@Bean
//	public FilterRegistrationBean csrfFilterRegistration() {
//		FilterRegistrationBean registration = new FilterRegistrationBean();
//		List<Pattern> patterns = new ArrayList<Pattern>();
//		patterns.add(Pattern.compile(SecurityConstant.LOGIN_PATH));
//		patterns.add(Pattern.compile(SecurityConstant.LOGOUT_PATH));
//		patterns.add(Pattern.compile("/openHTTP/*"));
//		patterns.add(Pattern.compile("/favicon.ico"));
//		patterns.add(Pattern.compile("/openWS/*"));
//		patterns.add(Pattern.compile("/openSocket/*"));
//		registration.setFilter(myCsrfFilter(patterns));//添加过滤器
//		registration.addUrlPatterns("/*");//设置过滤路径，/*所有路径
//		registration.setName("myCsrfFilter");//设置优先级
//		registration.setOrder(2);//设置优先级
//		return registration;
//	}
//	
//	@Bean(name = "myAuthenticationFilter")
//	public MyAuthenticationFilter myAuthenticationFilter(List<Pattern> patterns){
//		return new MyAuthenticationFilter(patterns);
//	}
//	@Bean(name = "myCsrfFilter")
//	public CsrfFilter myCsrfFilter(List<Pattern> patterns){
//		return new CsrfFilter(patterns);
//	}
//	
//	@Autowired
//	@Qualifier(value = "defaultDataSource")
//	private DataSource defaultSource;
//
//	@Bean("jdbcTemplate")
//	@Primary
//	public JdbcTemplate jdbcTemplate(){
//		JdbcTemplate JdbcTemplate = new JdbcTemplate(defaultSource);
//		return JdbcTemplate;
//	}
//
//	@Bean("jdbcDao")
//	@Primary
//	public JdbcDao jdbcDao(){
//		JdbcDaoImpl jdbcDao = new JdbcDaoImpl();
//		jdbcDao.setJdbcTemplate(jdbcTemplate());
//		return jdbcDao;
//	}
//
//
//	@Autowired
//	private JedisConnectionFactory jedisConnectionFactory;
//
//	@Bean
//	@Primary
//	public StringRedisTemplate stringRedisTemplate() {
//		StringRedisTemplate template = new StringRedisTemplate(jedisConnectionFactory);
//		logger.info("StringRedisTemplate bean init success.");
//		return template;
//	}
//
//
//	@Bean
//	@Primary
//	public ObjectRedisTemplate objectRedisTemplate(){
//		ObjectRedisTemplate template = new ObjectRedisTemplate(jedisConnectionFactory);
//		logger.info("ObjectRedisTemplate bean init success.");
//		return template;
//	}
//
//	@Bean
//	public SpringContextUtils springContextUtils(){
//		return new SpringContextUtils();
//	}
//
//	@Bean
//	public HttpSessionStrategy httpSessionStrategy() {
//		HttpSessionStrategy strategy = new MyCookieHttpSessionStrategy();
//		return strategy;
//	}
//	
//	
//	@Bean
//	public ConfigureRedisAction configureRedisAction() {
//		return ConfigureRedisAction.NO_OP;
//	}
	
//	@ControllerAdvice(annotations = {RestController.class,Controller.class},
//			basePackages={"com.efast.dmom.equipment.controller","com.efast.cafe.commponent.*.controller"})
//	public class MyGlobalExceptionAdvice extends AbstractMappingJacksonResponseBodyAdvice{
//		@ExceptionHandler(value = Exception.class)
//		@ResponseBody
//		public ResultBean defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
//			logger.error("exec error", e);
//			return GlobalExceptionAdvice.defaultErrorHandler(req, e);
//		}
//		@Override
//		protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
//				MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
//			 if (ResultBean.class.isAssignableFrom(returnType.getParameterType())) {
//		            return;
//		        }
//			 	if(request instanceof org.springframework.http.server.ServletServerHttpRequest){
//			 		org.springframework.http.server.ServletServerHttpRequest req = 
//			 				(org.springframework.http.server.ServletServerHttpRequest)request;
//			 		String url = req.getServletRequest().getServletPath();
//			 		if(url.startsWith("/swagger-resources")
//			 				|| url.startsWith("/v2/api-docs")  
//			 				|| url.startsWith("/statemach")
//			 				|| url.startsWith("/frame_center/define")
//			 				|| url.startsWith("/schedule")
//			 				|| url.startsWith("/openHTTP")){
//			 			bodyContainer.setValue(bodyContainer.getValue());
//			 			return;
//			 		}
//			 		
//			 	}
//			 	ResultBean resultBean = new ResultBean();
//		        resultBean.setSuccess(true);
//		        resultBean.setObject(bodyContainer.getValue());
//		        bodyContainer.setValue(resultBean);
//			}
//	}
	
}
